package com.servlets;

import com.alibaba.fastjson.JSON;
import com.pojo.Order;
import com.pojo.OrderProduct;
import com.pojo.Product;
import com.service.*;
import com.service.impl.*;
import com.utils.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;

@WebServlet("/order.do")
public class OrderServlet extends BaseServlet{
    private OrderService service = new OrderServiceImpl();
    private OrderProductService orderProductService = new OrderProductServiceImpl();
    private ProductService productService = new ProductServiceImpl();

    public void add(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        //1.根据前台参数组装实体类
        Map<String, String[]> map = request.getParameterMap();
        Order order = JDBCUtil.mapToBean(map, Order.class);
        order.setId(UUID.randomUUID().toString().replace("-","").substring(0,10));
        order.setCreateTime(new Timestamp(new Date().getTime()));
        //2.获取json字符串，转换为对象
        String orderProductJson = request.getParameter("orderProductJson");
        List<OrderProduct> list = JSON.parseArray(orderProductJson, OrderProduct.class);
        //3.获取用户id
        String msg=null,userID = request.getParameter("userId");
        //4.根据用户id获取购物车id
        ShopCartService shopCartService = new ShopCartServiceImpl();
        String cartId = shopCartService.findByUserID(userID).getCartId();
        ShopCartProductService shopCartProductService = new ShopCartProductServiceImpl();
        if (service.addOrder(order)){
            try{
                for (OrderProduct orderProduct : list) {
                    //5.循环遍历添加订单商品表
                    orderProduct.setOrderId(order.getId());
                    orderProduct.setId(UUID.randomUUID().toString().replace("-","").substring(0,10));
                    orderProductService.addOPD(orderProduct);
                    //6.根据购物车id删除购物车商品表
                    shopCartProductService.deleteByCartIDAndPID(cartId,orderProduct.getProductId());
                }
                msg = "结算成功";
            }catch (Exception e) {
                msg = "结算失败";
                e.printStackTrace();
            }finally {
                response.getWriter().write(JSON.toJSONString(msg));
            }

        }
    }

    public void delete(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        response.setContentType("application/json;charset=utf-8");
        String id = request.getParameter("id"),msg;
        if (id!=null&&id.length()>0){
            if(service.deleteByID(id))
                msg = "删除成功";
            else
                msg = "删除失败";
        }else
            msg = "id为空";
        response.getWriter().write(JSON.toJSONString(msg));
    }

    public void findByUserID(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        response.setContentType("application/json;charset=utf-8");
        String userID = request.getParameter("userID");
        //1.根据userID获取所有该用户所有订单
        List<Order> orderList = service.findByUserID(userID);

        for (Order order : orderList) {
            //2.根据该用户订单查询订单商品
            List<OrderProduct> orderProductList = orderProductService.findByOrderID(order.getId());

            //3.根据订单商品表中的id查询出商品
            for (OrderProduct orderProduct : orderProductList) {
                orderProduct.setProduct(productService.findByID(orderProduct.getProductId()));
            }

            //4.将订单商品表添加到订单类
            order.setOrderProducts(orderProductList);
        }
        response.getWriter().write(JSON.toJSONString(orderList));
    }
}
